﻿using System;
using System.IO;
using System.IO.Packaging;

namespace CLeopardZip
{
    public class ZipHandler
    {
        public static void Compress(FileInfo fi, DirectoryInfo dir)
        {
            if (fi.Exists)
            {
                fi.Delete();
            }

            Package zipFilePackage = ZipPackage.Open(fi.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            foreach (FileInfo physicalFile in dir.GetFiles())
            {
                string physicalfilePath = physicalFile.FullName;

                Uri partUri = PackUriHelper.CreatePartUri(new Uri(physicalFile.Name, UriKind.Relative));

                PackagePart newFilePackagePart = zipFilePackage.CreatePart(partUri, System.Net.Mime.MediaTypeNames.Text.Xml);

                byte[] fileContent = File.ReadAllBytes(physicalfilePath);

                newFilePackagePart.GetStream().Write(fileContent, 0, fileContent.Length);
            }

            foreach (DirectoryInfo subDir in dir.GetDirectories())
            {
                foreach (FileInfo physicalFile in subDir.GetFiles())
                {
                    string physicalfilePath = physicalFile.FullName;

                    Uri partUri = PackUriHelper.CreatePartUri(new Uri(subDir.Name + "/" + physicalFile.Name, UriKind.Relative));

                    PackagePart newFilePackagePart = zipFilePackage.CreatePart(partUri, System.Net.Mime.MediaTypeNames.Text.Xml);

                    byte[] fileContent = File.ReadAllBytes(physicalfilePath);

                    newFilePackagePart.GetStream().Write(fileContent, 0, fileContent.Length);
                }
            }

            zipFilePackage.Close();
        }

        public static bool Decompress(FileInfo fi, string origName)
        {
            bool returnVal = false;

            string curFile = fi.FullName;

            Package zipFilePackage = ZipPackage.Open(curFile, FileMode.Open, FileAccess.ReadWrite);

            foreach (ZipPackagePart contentFile in zipFilePackage.GetParts())
            {
                CreateFile(origName, contentFile);
                returnVal = true;
            }

            zipFilePackage.Close();

            return returnVal;
        }

        private static void CreateFile(string rootFolder, ZipPackagePart contentFile)
        {
            // Initially create file under the folder specified
            string contentFilePath = string.Empty;
            contentFilePath = contentFile.Uri.OriginalString.Replace('/',
                             System.IO.Path.DirectorySeparatorChar);

            if (contentFilePath.StartsWith(
                System.IO.Path.DirectorySeparatorChar.ToString()))
            {
                contentFilePath = contentFilePath.TrimStart(
                                         System.IO.Path.DirectorySeparatorChar);
            }
            else
            {
                //do nothing
            }

            contentFilePath = System.IO.Path.Combine(rootFolder, contentFilePath);
            //contentFilePath =  System.IO.Path.Combine(rootFolder, contentFilePath); 

            //Check for the folder already exists. If not then create that folder

            if (System.IO.Directory.Exists(
                System.IO.Path.GetDirectoryName(contentFilePath)) != true)
            {
                System.IO.Directory.CreateDirectory(
                          System.IO.Path.GetDirectoryName(contentFilePath));
            }
            else
            {
                //do nothing
            }

            System.IO.FileStream newFileStream =
                    System.IO.File.Create(contentFilePath);
            newFileStream.Close();
            byte[] content = new byte[contentFile.GetStream().Length];
            contentFile.GetStream().Read(content, 0, content.Length);
            System.IO.File.WriteAllBytes(contentFilePath, content);
        }
    }
}